package com;

import java.io.DataInputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class HttpHandlerThread implements Runnable {
	private Socket socket;

	public HttpHandlerThread(Socket client) {
		socket = client;
		new Thread(this).start();
	}

	public void run() {
		try {
			// 读取客户端数据
			DataInputStream input = new DataInputStream(socket.getInputStream());
			String data = input.readLine();
			if (data != null) {
				String[] info = data.split(" ");
				if (info != null && info.length >= 3) {
					String url = data.substring(info[0].length() + 1,
							data.length() - info[info.length - 1].length() - 1);

					url = url.substring(14);
					Main.chromeUrl = url;
				}
			}

			while (data != null && !data.equals("")) {
				data = input.readLine();
			}

			OutputStream out = socket.getOutputStream();
			out.write("HTTP/1.1 200 OK\r\n".getBytes());
			out.write("Server: JSP2/1.0.27\r\n".getBytes());
			SimpleDateFormat sdf = new SimpleDateFormat();
			out.write(("Date: " + (sdf.format(new Date())) + "\r\n").getBytes());
			out.write("Content-Type: text/html\r\n".getBytes());
			out.write("Content-Length: 2\r\n".getBytes());
			out.write("Connection: Keep-Alive\r\n".getBytes());
			out.write("\r\n".getBytes());
			out.write("OK".getBytes());

			out.flush();
			out.close();
			input.close();
		} catch (Exception e) {
		} finally {
			if (socket != null) {
				try {
					socket.close();
				} catch (Exception e) {
					socket = null;
				}
			}
		}
	}
}
